package com.jader.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jader.beans.CollectStore;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

public class CollectAdapter extends BaseAdapter {
	List<CollectStore> collect_store_list = new ArrayList<CollectStore>();
	Context context;
	private LayoutInflater inflater;
	private HttpUtils httpUtils;
	private String url = JaderApplication.getIP() + "/Jader/client";

	public CollectAdapter(List<CollectStore> list, Context context) {
		super();
		this.collect_store_list = list;
		this.context = context;
		inflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return collect_store_list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return collect_store_list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	ViewHolder viewHolder;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.listview_collect, null);
			viewHolder = new ViewHolder();
			viewHolder.img_store_pic = (ImageView) convertView
					.findViewById(R.id.img_listview_collect_storepic);
			viewHolder.img_collect_more = (ImageView) convertView
					.findViewById(R.id.img_collect_more);
			viewHolder.tv_store_name = (TextView) convertView
					.findViewById(R.id.tv_listview_collect_storename);
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		String store_name = collect_store_list.get(position).getSeller()
				.getSellername();
		viewHolder.tv_store_name.setText(store_name);
		String store_picture = collect_store_list.get(position).getSeller()
				.getPicture();
		JaderApplication.getBitmapUtils().display(viewHolder.img_store_pic,
				JaderApplication.getIP() + "/Jader" + store_picture);
		viewHolder.img_collect_more.setOnClickListener(new Position(position));
		return convertView;
	}

	class ViewHolder {
		ImageView img_store_pic, img_collect_more;
		TextView tv_store_name;
	}

	class Position implements OnClickListener {
		int z;

		public Position(int i) {
			this.z = i;
		}

		@Override
		public void onClick(View v) {
			deleteStore(z);
		}

	}

	// 取消店铺收藏
	public void deleteStore(final int local) {
		new com.jader.myviews.AlertDialog(context).builder()
				.setTitle("是否删除店铺收藏?")
				.setPositiveButton("确定", new OnClickListener() {

					@Override
					public void onClick(View v) {
						String storeId = ""
								+ collect_store_list.get(local)
										.getCollectStoreId();
						httpUtils = new HttpUtils();
						RequestParams params = new RequestParams();
						params.addBodyParameter("type", ""
								+ JaderApplication.COLLECT_STORE_DELETE);
						params.addBodyParameter("storeid", storeId);
						httpUtils.send(HttpMethod.POST, url, params,
								new RequestCallBack<String>() {

									@Override
									public void onFailure(HttpException arg0,
											String arg1) {
										JaderApplication.show(context, "取消失败");
									}

									@Override
									public void onSuccess(
											ResponseInfo<String> arg0) {
										JaderApplication.show(context, "取消成功");
										collect_store_list.remove(local);
										notifyDataSetChanged();
									}
								});
					}
				}).setNegativeButton("取消", null).show();

	}
}
